package com.zyx.javademo.juc.scheduled;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Yaxi.Zhang
 * @since 2021/6/10 09:51
 * desc: Timer案例
 */
public class TimerDemo {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    // 系统睡5s钟, 而 ScheduledExecutorService 中指定每隔 2s 执行一次
                    // 观察 2s 后还是 5s 后执行输出语句
                    Thread.sleep(5000);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+" run : "+ System.currentTimeMillis());
            }
        }, 0, 2000);
    }
}
